﻿/*************************************************/
function ExMaps() { };
ExMaps.toPixelString = function(pixels) {
    return Math.round(pixels) + "px"
};
//**********************Extent Marker From GMarker*******************//
function ExMarker(point, icon, insert, iconContent, infoWindowContent) {

    this.point_ = point;
    this.icon_ = icon;

    this.insert_ = insert;
    this.iconContent_ = iconContent;
    this.infoWindowContent_ = infoWindowContent;

    ExMarker.prototype._initialize = ExMarker.prototype.initialize;
    ExMarker.prototype.initialize = function(map) {

        this.map = map;
        this._initialize(map);
        this.setPoint(this.point_);
        if (this.iconContent_) {
            this.initializeIconContentElement();
            if (this.getIcon().contentAnchor) {
                var doc = this.map.getContainer().ownerDocument;
                var td = doc.createElement('td');
                var tr = doc.createElement('tr');
                var tbody = doc.createElement('tbody');
                var table = doc.createElement('table');
                table.style.border = '0';
                table.style.borderCollapse = 'collapse';
                table.style.width = ExMaps.toPixelString(this.icon.contentSize.width);
                table.style.height = ExMaps.toPixelString(this.icon.contentSize.height);
                td.style.textAlign = 'center';
                td.style.verticalAlign = 'middle';
                td.style.border = '0';
                td.style.borderSpacing = '0';
                this.iconContentElement.margin = '0 auto';
                this.iconContentElement.position = 'relative';

                this.map.getPane(G_MAP_MARKER_PANE).appendChild(table);
                table.appendChild(tbody);
                tbody.appendChild(tr);
                tr.appendChild(td);
                td.appendChild(this.iconContentElement_);
                this.iconContentElement_ = table;
            }
            else {
                this.map.getPane(G_MAP_MARKER_PANE).appendChild(this.iconContentElement_)
            }
        }
        if (this.infoWindowContent_) {
            GEvent.bind(this, "click", this, this.displayInfoWindowContent);
        }
    };

    ExMarker.prototype.displayInfoWindowContent = function() {
        if (typeof this.infoWindowContent_ == 'string') {
            this.openInfoWindowHtml(this.infoWindowContent_);
        } else if (typeof this.infoWindowContent_ == 'object') {
            this.openInfoWindow(this.infoWindowContent_);
        } else if (typeof this.infoWindowContent_ == 'function') {
            this.displayInfoWindowContent(this.infoWindowContent_(this));
        }
    };

    ExMarker.prototype.initializeIconContentElement = function() {
        if (typeof this.iconContent_ == 'string') {
            this.iconContentElement_ = this.map.getContainer().ownerDocument.createElement('div');
            this.iconContentElement_.innerHTML = this.iconContent_;
            this.iconContentElement_.style.fontFamily = "verdana, sans-serif";
            this.iconContentElement_.style.fontWeight = "bold";
            this.iconContentElement_.style.color = "red";
        } else if (typeof this.iconContent_ == 'object') {
            this.iconContentElement_ = this.iconContent_;
        } else if (typeof this.iconContent_ == 'function') {
            this.initializeIconContentElement(this.iconContent_(this));
        }
    };

    ExMarker.prototype._redraw = ExMarker.prototype.redraw;
    ExMarker.prototype.redraw = function(force) {
        if (!force) return;
        this._redraw(force);
        var divCoord = this.map.fromLatLngToDivPixel(this.getPoint());
        var left = divCoord.x - this.getIcon().iconAnchor.x;
        var top = divCoord.y - this.getIcon().iconAnchor.y;

        if (this.iconContentElement_) {
            this.iconContentElement_.style.position = "absolute";
            if (this.getIcon().contentAnchor) {
                this.iconContentElement_.style.left = ExMaps.toPixelString(left + this.getIcon().contentAnchor.x);
                this.iconContentElement_.style.top = ExMaps.toPixelString(top + this.getIcon().contentAnchor.y);
            }
            else {
                this.iconContentElement_.style.left = ExMaps.toPixelString(left);
                this.iconContentElement_.style.top = ExMaps.toPixelString(top);
            }
        }
    };

    ExMarker.prototype._remove = ExMarker.prototype.remove;
    ExMarker.prototype.remove = function() {
        this._remove();
        if (this.iconContentElement_) {
            this.map.getPane(G_MAP_MARKER_PANE).removeChild(this.iconContentElement_)
        }
    };

    ExMarker.prototype.copy = function() {
        return new ExMarker(this.point_, this.icon_, this.insert_, this.iconContent_, this.infoWindowContent_)
    };
}